package com.jsonal.controller;

import com.jsonal.entity.manager.LoginBean;
import com.jsonal.form.manager.LoginForm;
import com.jsonal.service.manager.LoginService;
import com.jsonal.util.RedisUtil;
import com.jsonal.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: jingji.lin
 * @Description: 主页
 * @Date: 2020/4/12 14:16
 * @Version: 1.0
 */
@Controller
public class IndexController extends BaseController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping(value = "/managers/login", method = RequestMethod.POST)
    @ApiOperation(value = "/managers/login", httpMethod = "POST", notes = "登录")
    @ResponseBody
    public Result login(@RequestBody LoginForm loginForm) {
        /**
         * 登录：判断用户名密码不为空，然后验证密码
         */
        LoginBean loginBean = new LoginBean();
        this.setFormToBean3(loginForm, loginBean);
        return loginService.login(loginBean);
    }

    @RequestMapping(value = "/managers/tokenIsExists/{token}", method = RequestMethod.GET)
    @ResponseBody
    public Result tokenIsExists(@PathVariable("token") String token) {
        boolean key = redisUtil.hasKey(token);
        if (key) {
            return new Result(true, "token已存在");
        }
        return new Result(false, "token不存在");
    }

    @RequestMapping(value = "/managers/updateToken/{token}", method = RequestMethod.POST)
    @ResponseBody
    public Result updateToken(@PathVariable("token") String token) {
        if (redisUtil.hasKey(token)) {
            redisUtil.del(token);
            redisUtil.set(token, System.currentTimeMillis(), 60*30);
            return new Result(true, "更新成功");
        }
        return  new Result(false, "token不存在");
    }

    @RequestMapping(value = "/blog", method = RequestMethod.GET)
    public String showBlogView() {
        return "blog/dist/index.html";
    }

    @RequestMapping(value = "/managers", method = RequestMethod.GET)
    public String showManagerView() {
        return "manager/dist/index.html";
    }
}
